package tp.pr3.instructions;

import tp.pr3.NavigationModule;
import tp.pr3.RobotEngine;
import tp.pr3.Rotation;
import tp.pr3.instructions.exceptions.InstructionExecutionException;
import tp.pr3.instructions.exceptions.WrongInstructionFormatException;
import tp.pr3.items.ItemContainer;
import tp.pr3.util.ErrorMessages;
import tp.pr3.util.InstructionParser;

public class TurnInstruction implements Instruction {
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		//esto debería hacerse con un nuevo diccionario de instrucciones para turn (left y right como subcomandos)
		//pero dado que es fácil cambiarlo y no se va a hacer, lo dejamos así
		String inputId = InstructionParser.getArg(InstructionParser.parseCommand(cad, CmdDic.turnCommand)).toUpperCase();
		switch (inputId) {
			case CmdDic.turnLeftCommand: this.rotation = Rotation.LEFT; break;
			case CmdDic.turnRightCommand: this.rotation = Rotation.RIGHT; break;
			default: throw new WrongInstructionFormatException(ErrorMessages.IEE_RUNTIMEERROR);//this.rotation = Rotation.UNKNOWN;
		}
		return this;
	}

	@Override
	public String getHelp() {
		return CmdDic.turnHelp;
	}

	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) {
		this.navigationModule = navigation;
		this.robotEngine = engine;
	}

	@Override
	public void execute() throws InstructionExecutionException {
		if (this.rotation == Rotation.UNKNOWN)
			throw new InstructionExecutionException(ErrorMessages.IEE_RUNTIMEERROR);
		this.navigationModule.rotate(this.rotation);
		this.robotEngine.sayAndUpdateDataAfterFuelConsumingAction(FUEL_DECREMENT_TURN);
	}

	private static final int FUEL_DECREMENT_TURN = -5;
	private Rotation rotation;

	private NavigationModule navigationModule;
	private RobotEngine robotEngine;
}
